package logic

import (
	"context"
	"store/models/mysql"

	"store/storerpc/internal/svc"
	"store/storerpc/storerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreListLogic {
	return &StoreListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreListLogic) StoreList(in *storerpc.StoreListRequest) (*storerpc.StoreListResponse, error) {
	// todo: add your logic here and delete this line
	var store []mysql.Store
	offset := (in.Page - 1) * in.PageSize
	query := mysql.Db.Debug().Offset(int(offset)).Limit(int(in.PageSize))
	if in.Shopkeeper != "" {
		query = query.Debug().Where("shopkeeper = ?", "%"+in.Shopkeeper+"%")
	}
	query.Model(&store).Order("price desc").Find(&store)
	var slice []*storerpc.StoreList
	for _, v := range store {
		list := storerpc.StoreList{
			Id:         int64(v.ID),
			StoreName:  v.StoreName,
			Nickname:   v.Nickname,
			Address:    v.Address,
			Price:      v.Price,
			Shopkeeper: v.Shopkeeper,
		}
		slice = append(slice, &list)
	}
	return &storerpc.StoreListResponse{List: slice}, nil
}
